/**
 * 同站线路协议工具类
 */
import ajaxAsync from '../../../utils/ajaxAsync';
import { storeLineCollection } from '../confs/keyConfs';
import Line from '../models/line'
import StoreLine from '../models/store-line'
import XiaobuStorage from '../../../utils/XiaobuStorage';

/**
 * 获取当前站点经过的线路列表
 * @param {String} startStationId 站点id
 * @param {Number} index 页码
 * @param {Number} pageSize 单页数据长度
 * @returns {Promise<{length: number, list: Array[]}>}
 */
async function getLineList (startStationId, index, pageSize) {
  let res = await ajaxAsync('/bus/busWithDirectionBystation', {
    IMPORT: startStationId,
    PAGE_INDEX: index,
    PAGE_CAPACITY: pageSize
  })
  if (res) {
    let length = res.COUNT || 0
    // 缓存中的收藏信息（弃用）
    let collectedLineArr
    try {
      collectedLineArr = await XiaobuStorage.getItem(storeLineCollection)
    } finally {
      collectedLineArr = collectedLineArr || []
    }

    let collectedLineIdArr = collectedLineArr.map(
      i => i.SUBROUTE_ID + '' + i.TYPE
    )
    // 数据封装
    let list = res.LIST.map((item) => {
      let array = []
      // 下行线路
      let downBusesInfo = item.DOWNBUSESINFO
      let downName
      let collectedDown
      let downId
      if (downBusesInfo) {
        downName = downBusesInfo.BUS_NAME
        downName = /^[0-9]*$/.test(downName) ? downName + '路' : downName
        downId = downBusesInfo.SUB_ROUTEID + '' + downBusesInfo.TYPE
        collectedDown = collectedLineIdArr.includes(downId)
        array.push(
          new Line(
            downId,
            downName,
            downBusesInfo.BUS_START_STATION,
            downBusesInfo.BUS_END_STATION,
            downBusesInfo.NOWBUS_STOP,
            new StoreLine(downBusesInfo),
            collectedDown
          )
        )
      }
      // 上行线路
      let upBusesInfo = item.UPBUSESINFO
      let upName
      let collectedUp
      let upId
      console.log(upBusesInfo)
      if (upBusesInfo) {
        upName = upBusesInfo.BUS_NAME
        upName = /^[0-9]*$/.test(upName) ? upName + '路' : upName
        upId = upBusesInfo.SUB_ROUTEID + '' + upBusesInfo.TYPE
        collectedUp = collectedLineIdArr.includes(upId)
        array.push(
          new Line(
            upId,
            upName,
            upBusesInfo.BUS_START_STATION,
            upBusesInfo.BUS_END_STATION,
            upBusesInfo.NOWBUS_STOP,
            new StoreLine(upBusesInfo),
            collectedUp
          )
        )
      }
      return array
    }).sort((cur, next) => {
    // 收藏的线路置顶展示;无收藏线路，站点距离小排前面，若线路显示暂无车辆信息，以线路名由小到大排序
      if (cur && (cur[0].collected || (cur[1] && cur[1].collected))) {
        return -1
      } else if (
        next &&
      (next[0].collected || (next[1] && next[1].collected))
      ) {
        return 1
      } else if (cur && cur[0].nowStopNum === -99) {
        return -1
      } else if (next && next[0].nowStopNum === -99) {
        return 1
      } else if (
        cur &&
      next &&
      cur[0].nowStopNum >= 0 &&
      next[0].nowStopNum >= 0
      ) {
        return cur[0].nowStopNum - next[0].nowStopNum
      } else if (
        cur &&
      next &&
      cur[0].nowStopNum >= 0 &&
      next[0].nowStopNum < 0
      ) {
        return -1
      } else if (
        cur &&
      next &&
      next[0].nowStopNum >= 0 &&
      cur[0].nowStopNum < 0
      ) {
        return 1
      } else if (
        cur &&
      next &&
      cur[0].nowStopNum < 0 &&
      next[0].nowStopNum < 0
      ) {
        let prevName = cur[0].name
        let nextName = next[0].name
        let prevNum = /^[0-9]*$/.test(prevName.slice(0, prevName.length - 1))
          ? parseFloat(prevName.slice(0, prevName.length - 1))
          : NaN
        let nextNum = /^[0-9]*$/.test(nextName.slice(0, nextName.length - 1))
          ? parseFloat(nextName.slice(0, nextName.length - 1))
          : NaN
        if (
          prevName.endsWith('路') &&
        nextName.endsWith('路') &&
        !Number.isNaN(prevNum) &&
        !Number.isNaN(nextNum)
        ) {
          return prevNum - nextNum
        } else {
          return prevName.localeCompare(nextName, 'zh')
        }
      }
    })
    return {
      list,
      length
    }
  }
}

export { getLineList }